#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Version : Python 3.6

"""
功 能：垃圾分类信息
版权信息：华为技术有限公司，版本所有(C) 2019-2029
"""

import json
import requests

rubbish_info = {0: "分类投放可回收物时应保持清洁干燥，避免污染，轻投轻放；其中废纸尽量平整；立体包装物请清空内容物，清洁后压扁投放；有尖锐边角的，应包裹后投放。",
                1: "充电电池、纽扣电池、蓄电池投放时应注意轻放；油漆桶、杀虫剂如有残留请密闭后投放；荧光灯、节能灯易破损连带包装或包裹后投放；废药品及其包装应一并投放进垃圾桶中。",
                2: "有包装物的厨余垃圾应取出后分类投放；大块骨头和椰子壳、榴莲壳等不易生化降解，作为干垃圾进行投放；纯液体（如牛奶等）食物垃圾可直接倒入下水口；其余应沥干水分后再投放进厨余垃圾桶。",
                3: "干垃圾应尽量沥干水分，投入干垃圾容器内。",
                }

def get_response(word, mode=1):
    key = "5dfe85beb3c0ed5b5bcdcac331008291"
    url = "http://api.tianapi.com/lajifenlei/index"
    data = {
        "key": key,
        "word": word,
        "mode": mode,
    }
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    res = requests.post(url, data=data, headers=headers, verify=False)
    return res.json()

def handler(event, context):
    slot_temp = json.dumps(event, ensure_ascii=False)
    slot_info = json.loads(slot_temp)
    logg = context.getLogger()
    logg.info(slot_temp)
    word = slot_info.get("goods", None)
    result = {"content": "unknown"}
    if word:
        res = get_response(word)
        if res['code'] != 200:
            result["content"] = "数据源资源不足！"
            return json.dumps(result, ensure_ascii=False)
        all_data_list = res.get("newslist", [{}])
        filter_data_list = list(filter(lambda x: x.get("name", "") == word, all_data_list))
        if filter_data_list:
            _type = filter_data_list[0].get("type", 4)
            rubbish_result = ""
            logg.info(word)
            if _type == 0:
                rubbish_result = "{}是可回收垃圾。投放建议：{}".format(word, rubbish_info[_type])
            elif _type == 1:
                rubbish_result = "{}是有害垃圾。投放建议：{}".format(word, rubbish_info[_type])
            elif _type == 2:
                rubbish_result = "{}是厨余垃圾（即湿垃圾）。投放建议：{}".format(word, rubbish_info[_type])
            elif _type == 3:
                rubbish_result = "{}是其他垃圾（即干垃圾）。投放建议：{}".format(word, rubbish_info[_type])
            else:
                pass
            if rubbish_result:
                result["content"] = rubbish_result
    logg.info(json.dumps(result, ensure_ascii=False))
    return json.dumps(result, ensure_ascii=False)


if __name__ == '__main__':
    event = {
        "goods": "茶叶渣",
        }
    print(handler(event, None))

